package com.hw.ui.config;

import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.hw.HWApp;
import com.hw.base.model.BaseModel;
import com.hw.base.util.Util;
import com.hw.bean.AbBean;
import com.hw.bean.AbProductBean;
import com.hw.daobean.DataBaseControl;
import com.hw.daobean.ProductInfo;

import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

public class ConfigModelImpl extends BaseModel implements ConfigModel {
    private String tag = "ConfigModelImpl";
    private String msg = "";
    private AbBean abBean;
    private AbProductBean abProductBean;

    public void postHandle(final CallBack listener, final int n) {
        new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                switch (n) {
                    case -1:
                        listener.onFail(msg);
                        break;
                    case 0:
                        listener.onconfigSuccess(msg);
                        break;
                    case 1:
                        listener.ondownload(msg);
                        break;
                    default:
                        break;
                }
            }
        });
    }

    @Override
    public void config(final String url, final String cid, final CallBack listener) {
        try {
            Request request = new Request.Builder().url(url + Util.urlsuffix + "?action=CheckNet&cid=" + cid).build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    msg = Util.netConnectionError;
                    postHandle(listener, -1);
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        String result = response.body().string();
                        Log.e(tag, result);
                        abBean = gson.fromJson(result, AbBean.class);
                        if (null != abBean && abBean.getErrcode() == 0) {
                            msg = "测试连接成功";
                            HWApp.sEditor.putString(Util.addk, url);
                            HWApp.sEditor.putString(Util.cidk, cid);
                            HWApp.sEditor.commit();
                            postHandle(listener, 0);
                        } else {
                            msg = abBean.getErrmsg();
                            postHandle(listener, -1);
                        }
                    } else {
                        msg = Util.netServerError;
                        postHandle(listener, -1);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            msg = Util.exceptionError;
            postHandle(listener, -1);
        }
    }

    @Override
    public void download(final CallBack listener) {
        try {
            Request request = new Request.Builder().url(
                    HWApp.sSharedPreferences.getString(Util.addk, "") + Util.urlsuffix +
                            "?action=DownloadBaseInfo&cid=" +
                            HWApp.sSharedPreferences.getString(Util.cidk, "") +
                            "&userID=" + "0" +
                            "&baseType=" + "1").build();
            client.newCall(request).enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    msg = Util.netConnectionError;
                    postHandle(listener, -1);
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    if (response.isSuccessful()) {
                        String result = response.body().string();
                        Log.e(tag, result);
                        DataBaseControl.deleteProduct();
                        abProductBean = gson.fromJson(result, AbProductBean.class);
                        if (null != abProductBean && abProductBean.getErrcode() == 0 && abProductBean.getData()!=null) {
                            for (int i = 0; i <abProductBean.getData().size() ; i++) {
                                ProductInfo productInfo = new ProductInfo(null
                                ,abProductBean.getData().get(i).getId()
                                ,abProductBean.getData().get(i).getCode()
                                ,abProductBean.getData().get(i).getName()
                                ,abProductBean.getData().get(i).getCaseBoxNums()
                                ,abProductBean.getData().get(i).getOtherCode());
                                DataBaseControl.insertProduct(productInfo);
                            }
                            msg = "下载"+abProductBean.getData().size()+"条产品信息";
                            postHandle(listener, 1);
                        } else {
                            msg = abBean.getErrmsg();
                            postHandle(listener, -1);
                        }
                    } else {
                        msg = Util.netServerError;
                        postHandle(listener, -1);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            msg = Util.exceptionError;
            postHandle(listener, -1);
        }
    }
}

